package com.cloudlead.forwardAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseShipRoute;
import com.cloudlead.commonData.vo.BaseShipRoutePrice;
import com.cloudlead.forwardAction.service.BaseShipRoutePriceEditSer;
import com.cloudlead.forwardService.vo.BaseShipRoutePriceCreateVo;
import com.cloudlead.forwardService.vo.BaseShipRoutePriceVo;
import org.joda.time.LocalDate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/19.
 */
@Service
public class BaseShipRoutePriceEditSerImpl implements BaseShipRoutePriceEditSer {

    @Autowired
    BaseShipRoutePriceService baseShipRoutePriceService;
    @Autowired
    BaseCompanyService  baseCompanyService;
    @Autowired
    BaseDockService baseDockService;
    @Autowired
    BaseShipRouteService baseShipRouteService;
    @Autowired
    RegionService regionService;
    @Autowired
    TbDictionaryService tbDictionaryService;

    @Override
    /**
     * 航线报价创建
     */
    public String createBaseShipRoutePrice(BaseShipRoutePriceCreateVo cvo) {
        BaseShipRoutePrice bsrp=new BaseShipRoutePrice();
        BeanUtils.copyProperties(cvo, bsrp);//拷贝相同字段
        /**
         * 不同字段处理
         */
        if(cvo.getBaseCompany()!=null&&(!"".equals(cvo.getBaseCompany()))) {//公司
            BaseCompany bc=new BaseCompany();
                bc.setId(cvo.getBaseCompany());
            bsrp.setBaseCompany(bc);
        }
        if(cvo.getBaseShipRoute()!=null&&(!"".equals(cvo.getBaseShipRoute()))){//航线
            BaseShipRoute bsr = new BaseShipRoute();
                bsr.setId(cvo.getBaseShipRoute());
            bsrp.setBaseShipRoute(bsr);
        }
        if(cvo.getShipOwnerCompa()!=null&&(!"".equals(cvo.getShipOwnerCompa()))){//船公司
            BaseCompany bc = new BaseCompany();
                bc.setId(cvo.getShipOwnerCompa());
            bsrp.setShipOwnerCompa(bc);
        }
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        bsrp.setFlag("0");
        bsrp.setExpiresEnd(LocalDate.fromDateFields(cvo.getExpiresEnd()));
        bsrp.setExpiresStart(LocalDate.fromDateFields(cvo.getExpiresStart()));
        baseShipRoutePriceService.create(bsrp);
        return "success";
    }


    @Override
    /**
     * 航线报价查询
     */
    public Page<BaseShipRoutePriceVo> findAllBaseShipRoutePrice(BaseShipRoutePriceVo vo, int size, int page) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        //查询条件
        if(vo!=null) {
            if(vo.getId()!=null&&(!"".equals(vo.getId()))){
                tr.add(new Term("id",vo.getId()));
            }
            if(vo.getBaseCompany()!=null&&(!"".equals(vo.getBaseCompany()))){//公司
                tr.add(new Term("baseCompanyId",vo.getBaseCompany()));
            }
            if(vo.getBaseShipRoute()!=null&&(!"".equals(vo.getBaseShipRoute()))){//航线
                tr.add(new Term("baseShipRouteId",vo.getBaseShipRoute()));
            }
            if(vo.getShipOwnerCompany()!=null&&(!"".equals(vo.getShipOwnerCompany()))){//船公司
                tr.add(new Term("shipOwnerCompany",vo.getShipOwnerCompany()));
            }
            if(vo.getExpiresEnd()!=null&&(!"".equals(vo.getExpiresEnd()))){//有效期
                tr.add(new Term("expiresEnd",Operator.GE,vo.getExpiresEnd()));
            }
            if(vo.getStartPort()!=null&&(!"".equals(vo.getStartPort()))){ //起始港
                tr.add(new Term("baseShipRoute.startPort",vo.getStartPort()));
            }
            if(vo.getDestPort()!=null&&(!"".equals(vo.getDestPort()))){ //起始港
                tr.add(new Term("baseShipRoute.destPort",vo.getDestPort()));
            }
            if(vo.getBaseShipRouteName()!=null&&(!"".equals(vo.getBaseShipRouteName()))){ //起始港
                tr.add(new Term("baseShipRoute.baseShipRouteName",Operator.LK,vo.getBaseShipRouteName()));
            }

        }
        Filter filter=new Filter( new Criteria( tr));
             pr.setPage(page);//页码
             pr.setSize(size);//每页数量
             pr.setFilter(filter);
        Page<BaseShipRoutePrice> pa=baseShipRoutePriceService.findAll(pr);
        List<BaseShipRoutePrice> lpa=pa.getContent();
        Page<BaseShipRoutePriceVo> pavo = new Page<BaseShipRoutePriceVo>();
        List<BaseShipRoutePrice> lpa1 = new ArrayList<BaseShipRoutePrice>();
        for(Iterator<BaseShipRoutePrice> it = lpa.iterator();it.hasNext();) {//遍历
            BaseShipRoutePrice bsp = it.next();
                //航次
                BaseShipRoute bsr=baseShipRouteService.view(bsp.getBaseShipRoute().getId());
                bsp.setBaseShipRoute(bsr);
                //公司
                bsp.setBaseCompany(baseCompanyService.view(bsp.getBaseCompany().getId()));
            lpa1.add(bsp);
        }
        pa.setContent(lpa1);
        BeanUtils.copyProperties(pa, pavo);//拷贝page
        return pavo;
    }

    @Override
    /**
     * 航线报价删除
     */
    public String delBaseShipRoutePrice(String id) {
        BaseShipRoutePrice bsrp= baseShipRoutePriceService.view(id);
        if(bsrp!=null) {//查询到该记录
            bsrp.setFlag("1");
            baseShipRoutePriceService.update(bsrp);
        }
        else
            return "error ID";
        return "success";
    }

    @Override
    public void editBaseShipRoutePrice(BaseShipRoutePriceCreateVo vo) {
        BaseShipRoutePrice bsrp=baseShipRoutePriceService.view(vo.getId());
        BeanUtils.copyProperties(vo, bsrp);//拷贝相同字段
        bsrp.setFlag("0");
        bsrp.setExpiresEnd(LocalDate.fromDateFields(vo.getExpiresEnd()));
        bsrp.setExpiresStart(LocalDate.fromDateFields(vo.getExpiresStart()));
        baseShipRoutePriceService.update(bsrp);
    }


}
